﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
namespace Ledict.ConditionExtract.DAL
{
    public class SYS_Argument_ElementRelDAL
    {
        private string DbName;
        public SYS_Argument_ElementRelDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        public bool BatchAdd(IList<SYS_Argument_ElementRel> list)
        {
            StringBuilder strSql = new StringBuilder();
            foreach (var Model in list)
            {
                strSql.Append(string.Format(@" insert into SYS_Argument_ElementRel(AER_ID,CC_ID,XmlPath,UpdateTime) values('{0}','{1}','{2}','{3}') ",
                    Model.AER_ID, Model.CC_ID, Model.XmlPath, DateTime.Now));
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            if (oBase.ExecuteNonQuery(CommandType.Text, strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysArgumentElementRelDatas()
        {
            string sql = @" select AER_ID,CC_ID,XmlPath from SYS_Argument_ElementRel ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DataTable dt = oBase.ExecuteDataSet(CommandType.Text, sql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysArgumentElementRelData(string ccID)
        {
            string sql = string.Format(@" select AER_ID,CC_ID,XmlPath from SYS_Argument_ElementRel where CC_ID='{0}' ",ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DataTable dt = oBase.ExecuteDataSet(CommandType.Text,sql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddSysArgumentElementRelData(SYS_Argument_ElementRel model)
        {
            string sql = string.Format(@" insert into SYS_Argument_ElementRel(AER_ID,CC_ID,XmlPath,UpdateTime) values('{0}','{1}','{2}','{3}') ", model.AER_ID, model.CC_ID, model.XmlPath, DateTime.Now);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());            
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteSysArgumentElementRelData(string AER_ID)
        {
            string sql = string.Format(@" delete from SYS_Argument_ElementRel where AER_ID='{0}' ", AER_ID);            
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改一条数据
        /// </summary>
        public bool UpdateSysArgumentElementRelData(SYS_Argument_ElementRel model)
        {
            string sql = string.Format(@" update SYS_Argument_ElementRel set XmlPath='{0}',UpdateTime=GETDATE() where AER_ID='{1}' ", model.XmlPath,model.AER_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

    }
}
